﻿
using System;

using zijian666.Converts.Abstractions;
using zijian666.Converts.Convertor.Base;
using zijian666.Converts.Core;
using zijian666.Converts.Extensions;

namespace zijian666.Converts.Convertor;

/// <summary>
/// <seealso cref="IntPtr" /> 转换器
/// </summary>
public class IntPtrConvertor : FromConvertor<IntPtr>
                             , IFrom<int, IntPtr>
                             , IFrom<long, IntPtr>
                             , IFrom<object, IntPtr>
{
    /// <inheritdoc/>
    public ConvertResult<IntPtr> From(IConvertContext context, long input) => new IntPtr(input);

    /// <inheritdoc/>
    public ConvertResult<IntPtr> From(IConvertContext context, int input) => new IntPtr(input);

    /// <inheritdoc/>
    public ConvertResult<IntPtr> From(IConvertContext context, object input)
    {
        var result = context.Convert<long>(input);
        if (result.Success)
        {
            return new IntPtr(result.Value);
        }
        return context.ConvertFail(this, input, result.Exception);
    }
}
